<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>loggerserver.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_gears_loggerserver.js.html">loggerserver.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2007 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview This class lives on the main thread and takes care of incoming
<a name="line17"></a> * logger commands from a worker thread.
<a name="line18"></a> *
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.gears.LoggerServer&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line24"></a>goog.require(&#39;goog.debug.Logger&#39;);
<a name="line25"></a>goog.require(&#39;goog.debug.Logger.Level&#39;);
<a name="line26"></a>goog.require(&#39;goog.gears.Worker.EventType&#39;);
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * Creates an object that listens to incoming LOG commands and forwards them
<a name="line31"></a> * to a goog.debug.Logger
<a name="line32"></a> * @param {goog.gears.Worker} worker The worker thread that
<a name="line33"></a> *     we are managing the loggers on.
<a name="line34"></a> * @param {number} logCommandId The command id used for logging.
<a name="line35"></a> * @param {string} opt_workerName The name of the worker. If present then this
<a name="line36"></a> *     is added to the log records and to exceptions as {@code workerName}.
<a name="line37"></a> * @constructor
<a name="line38"></a> * @extends {goog.Disposable}
<a name="line39"></a> */
<a name="line40"></a>goog.gears.LoggerServer = function(worker, logCommandId, opt_workerName) {
<a name="line41"></a>  goog.Disposable.call(this);
<a name="line42"></a>
<a name="line43"></a>  /**
<a name="line44"></a>   * The command id to use to receive log commands from the workers.
<a name="line45"></a>   * @type {number}
<a name="line46"></a>   * @private
<a name="line47"></a>   */
<a name="line48"></a>  this.logCommandId_ = logCommandId;
<a name="line49"></a>
<a name="line50"></a>  /**
<a name="line51"></a>   * The worker thread object.
<a name="line52"></a>   * @type {goog.gears.Worker}
<a name="line53"></a>   * @private
<a name="line54"></a>   */
<a name="line55"></a>  this.worker_ = worker;
<a name="line56"></a>
<a name="line57"></a>  /**
<a name="line58"></a>   * The name of the worker.
<a name="line59"></a>   * @type {string}
<a name="line60"></a>   * @private
<a name="line61"></a>   */
<a name="line62"></a>  this.workerName_ = opt_workerName || &#39;&#39;;
<a name="line63"></a>
<a name="line64"></a>  /**
<a name="line65"></a>   * Message prefix containing worker ID.
<a name="line66"></a>   * @type {string}
<a name="line67"></a>   * @private
<a name="line68"></a>   */
<a name="line69"></a>  this.msgPrefix_ = &#39;[&#39; + worker.getId() + &#39;] &#39;;
<a name="line70"></a>
<a name="line71"></a>  // Listen for command&#39;s from the worker to handle the log command.
<a name="line72"></a>  worker.addEventListener(goog.gears.Worker.EventType.COMMAND,
<a name="line73"></a>                          this.onCommand_, false, this);
<a name="line74"></a>};
<a name="line75"></a>goog.inherits(goog.gears.LoggerServer, goog.Disposable);
<a name="line76"></a>
<a name="line77"></a>
<a name="line78"></a>/**
<a name="line79"></a> * Whether to show the ID of the worker as a prefix to the shown message.
<a name="line80"></a> * @type {boolean}
<a name="line81"></a> * @private
<a name="line82"></a> */
<a name="line83"></a>goog.gears.LoggerServer.prototype.useMessagePrefix_ = true;
<a name="line84"></a>
<a name="line85"></a>
<a name="line86"></a>/**
<a name="line87"></a> * @return {boolean} * Whether to show the ID of the worker as a prefix to the
<a name="line88"></a> *     shown message.
<a name="line89"></a> */
<a name="line90"></a>goog.gears.LoggerServer.prototype.getUseMessagePrefix = function() {
<a name="line91"></a>  return this.useMessagePrefix_;
<a name="line92"></a>};
<a name="line93"></a>
<a name="line94"></a>
<a name="line95"></a>/**
<a name="line96"></a> * Whether to prefix the message text with the worker ID.
<a name="line97"></a> * @param {boolean} b True to prefix the messages.
<a name="line98"></a> */
<a name="line99"></a>goog.gears.LoggerServer.prototype.setUseMessagePrefix = function(b) {
<a name="line100"></a>  this.useMessagePrefix_ = b;
<a name="line101"></a>};
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * Event handler for the command event of the thread.
<a name="line106"></a> * @param {goog.gears.WorkerEvent} e The command event sent by the the
<a name="line107"></a> *     worker thread.
<a name="line108"></a> * @private
<a name="line109"></a> */
<a name="line110"></a>goog.gears.LoggerServer.prototype.onCommand_ = function(e) {
<a name="line111"></a>  var message = /** @type {Array} */ (e.message);
<a name="line112"></a>  var commandId = message[0];
<a name="line113"></a>  if (commandId == this.logCommandId_) {
<a name="line114"></a>    var params = message[1];
<a name="line115"></a>    var i = 0;
<a name="line116"></a>    var name = params[i++];
<a name="line117"></a>
<a name="line118"></a>    // The old version sent the level name as well.  We no longer need it so
<a name="line119"></a>    // we just step over it.
<a name="line120"></a>    if (params.length == 5) {
<a name="line121"></a>      i++;
<a name="line122"></a>    }
<a name="line123"></a>    var levelValue = params[i++];
<a name="line124"></a>    var level = goog.debug.Logger.Level.getPredefinedLevelByValue(levelValue);
<a name="line125"></a>    if (level) {
<a name="line126"></a>      var msg = (this.useMessagePrefix_ ? this.msgPrefix_ : &#39;&#39;) + params[i++];
<a name="line127"></a>      var exception = params[i++];
<a name="line128"></a>      var logger = goog.debug.Logger.getLogger(name);
<a name="line129"></a>      var logRecord = logger.getLogRecord(level, msg, exception);
<a name="line130"></a>      if (this.workerName_) {
<a name="line131"></a>        logRecord.workerName = this.workerName_;
<a name="line132"></a>
<a name="line133"></a>        // Note that we happen to know that getLogRecord just references the
<a name="line134"></a>        // exception object so we can continue to modify it as needed.
<a name="line135"></a>        if (exception) {
<a name="line136"></a>          exception.workerName = this.workerName_;
<a name="line137"></a>        }
<a name="line138"></a>      }
<a name="line139"></a>      logger.logRecord(logRecord);
<a name="line140"></a>    }
<a name="line141"></a>    // ignore others for now
<a name="line142"></a>  }
<a name="line143"></a>};
<a name="line144"></a>
<a name="line145"></a>
<a name="line146"></a>/**
<a name="line147"></a> * Disposes of the logger server.
<a name="line148"></a> */
<a name="line149"></a>goog.gears.LoggerServer.prototype.disposeInternal = function() {
<a name="line150"></a>  goog.gears.LoggerServer.superClass_.disposeInternal.call(this);
<a name="line151"></a>
<a name="line152"></a>  // Remove the event listener.
<a name="line153"></a>  this.worker_.removeEventListener(
<a name="line154"></a>      goog.gears.Worker.EventType.COMMAND, this.onCommand_, false, this);
<a name="line155"></a>
<a name="line156"></a>  this.worker_ = null;
<a name="line157"></a>};
</pre>


</body>
</html>
